using System;
using System.Drawing;

namespace Niu
{
    public static class nThumb
    {
        private static Image Create(Image srcImage, Int32 thmbWidth, Int32 thmbHeight, Int32 picWidth, Int32 picHeight, Color BgColor)
        {
            Image thmbBitmap;
            Rectangle rectPic;
            Graphics g;

            thmbBitmap = new Bitmap(thmbWidth, thmbHeight);

            g = Graphics.FromImage(thmbBitmap);

            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

            g.Clear(BgColor);

            rectPic = new Rectangle((thmbBitmap.Width - picWidth) / 2, (thmbBitmap.Height - picHeight) / 2, picWidth, picHeight);

            g.DrawImage(srcImage, rectPic, new Rectangle(0, 0, srcImage.Width, srcImage.Height), GraphicsUnit.Pixel);

            return thmbBitmap;
        }

        public static Image Stretch(Image srcImage, Int32 thmbWidth, Int32 thmbHeight)
        {
            return Create(srcImage, thmbWidth, thmbHeight, thmbWidth, thmbHeight, Color.Black);
        }

        public static Image Zoom(Image srcImage, Int32 thmbWidth, Int32 thmbHeight, Color BgColor)
        {
            Int32 picWidth, picHeight;
    
            if (srcImage.Width >= srcImage.Height)
            {
                picWidth = thmbWidth;
                picHeight = (int)Math.Floor(Convert.ToDouble(srcImage.Height) * (Convert.ToDouble(thmbWidth) / Convert.ToDouble(srcImage.Width)));
            }
            else
            {
                picHeight = thmbHeight;
                picWidth = (int)Math.Floor(Convert.ToDouble(srcImage.Width) * (Convert.ToDouble(thmbHeight) / Convert.ToDouble(srcImage.Height)));
            }

            return Create(srcImage, thmbWidth, thmbHeight, picWidth, picHeight, BgColor);
        }

    }
}
